//
//  TMTQGCodeCell.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/21.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMTQGCodeCell.h"
@interface TMTQGCodeCell()<UITextFieldDelegate,UITextViewDelegate>
@end
@implementation TMTQGCodeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insert:) name:UITextViewTextDidChangeNotification object:nil];
    self.textView.layer.borderWidth = 1.0f;
    self.textView.layer.borderColor = [UIColor hx_colorWithHexRGBAString:@"EEEEEE"].CGColor;
    self.titleTF.delegate = self;
    self.textView.delegate = self;
}
- (IBAction)goMoneyBack:(UIButton *)sender {
    if (self.blocks) {
        self.blocks();
    }
}
- (void)insert:(UITextView *)textview{
    if (self.textView.text.length>0) {
        self.placeLabel.hidden = YES;
    }else{
        self.placeLabel.hidden = NO;
    }
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.titleTF) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.titleTF.text.length >= 20) {
            self.titleTF.text = [textField.text substringToIndex:20];
            return NO;
        }
    }
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if (textView == self.textView) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && text.length == 0) {
            return YES;
        }
        //so easy
        else if (self.textView.text.length >= 100) {
            self.textView.text = [textView.text substringToIndex:100];
            return NO;
        }
    }
    return YES;
}
//-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//    if ([text isEqualToString:@"\n"]) {
//        [textView resignFirstResponder];
//        return false;
//    }
//    return true;
//}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
@end
